from PIL import Image
from tqdm import tqdm

class PDFGenerator:
    @staticmethod
    def generate(image_paths, output_path):
        """生成PDF文件"""
        if not image_paths:
            return False
            
        print("\n开始生成PDF...")
        first_image = Image.open(image_paths[0])
        images = []
        
        for img_path in tqdm(image_paths[1:], desc="加载图片"):
            img = Image.open(img_path)
            if img.mode != 'RGB':
                img = img.convert('RGB')
            images.append(img)
        
        if first_image.mode != 'RGB':
            first_image = first_image.convert('RGB')
        
        print(f"正在保存PDF到: {output_path}")    
        first_image.save(output_path, save_all=True, append_images=images)
        print("PDF生成完成")
        return True